#include "mid_FSM.h"
#include "stdio.h"
#include "string.h"

extern bool Loop_flag;
extern TFT_UIManager g_UIManager;
extern UI_process_t g_UI_process_t[FSM_UI_COUNT];

static inline void FSM_Messages_ENTTER_handler(void)
{
    if (g_UIManager.current_screen->Cursor == 1)
    {
        UI_change_run(FSM_NONE);
    }
    UI_Measurement_Parameter_t *p = (UI_Measurement_Parameter_t *)g_UIManager.current_screen->Parameter;
    switch (p->UI_Measurement_Parameter)
    {
    // Channel Setup
    case 0:
        UI_change_run(FSM_Channel_Setup);
        break;
    // Set Averaging
    case 1:
        UI_change_run(FSM_Set_Averaging);
        break;
    // Resistivity
    case 2:
        UI_change_run(FSM_Resistivity);
        break;
    }
}

void FSM_Measurement_struct_create(void)
{
    UI_Measurement_Parameter_t *p = (UI_Measurement_Parameter_t *)g_UIManager.current_screen->Parameter;
    TFT_menu_ALL_Clear();
    Menu_WriteStr(1, 0, "Mesasurement Setup");
    g_UIManager.current_screen->Cursor = 0;
    p->UI_Measurement_Parameter = 0;
    TFT_menu_Arrow_write("<-");
}
void FSM_Measurement_handle_state_while(void)
{
    static uint8_t lastcursor = 0;
    char *Cursor_Buff[] = {"Channel Setup",
                           "Set Averaging",
                           "Resistivity"};
    UI_Measurement_Parameter_t *p = (UI_Measurement_Parameter_t *)g_UIManager.current_screen->Parameter;
    if (g_UIManager.current_screen->Cursor != lastcursor)
    {
        // 显示全部
        TFT_menu_Arrow_write("<-");
        Menu_WriteStr(2, 0, Cursor_Buff[p->UI_Measurement_Parameter]);
        goto exit;
    }
    if (Loop_flag)
    {
        if (g_UIManager.current_screen->Cursor == 0)
        {
            // 显示字
            Menu_WriteStr(2, 0, Cursor_Buff[p->UI_Measurement_Parameter]);
        }
        else
        {
            // 显示箭头
            TFT_menu_Arrow_write("<-");
        }
        Loop_flag = false;
    }
    else
    {
        if (g_UIManager.current_screen->Cursor == 0)
        {
            // 清除字
            Menu_ClearChar(2, 0, 16);
        }
        else
        {
            // 清除箭头
            TFT_menu_Arrow_Clear();
        }
        Loop_flag = true;
    }
exit:
    lastcursor = g_UIManager.current_screen->Cursor;
}

void FSM_Measurement_handle_event(FSM_Event_t event)
{
    UI_Measurement_Parameter_t *p = (UI_Measurement_Parameter_t *)g_UIManager.current_screen->Parameter;
    switch (event)
    {
    case FSM_Event_UP_PRESS_UP:
        p->UI_Measurement_Parameter = (p->UI_Measurement_Parameter + 1) % FSM_Messages_MAX;
        break;
    case FSM_Event_DOWN_PRESS_UP:
        p->UI_Measurement_Parameter = (p->UI_Measurement_Parameter + FSM_Messages_MAX - 1) % FSM_Messages_MAX;
        break;
    case FSM_Event_LEFT_PRESS_UP:
    case FSM_Event_RIGHT_PRESS_UP:
        g_UIManager.current_screen->Cursor = (g_UIManager.current_screen->Cursor + 1) % 2;
        break;
    case FSM_Event_ENTER_PRESS_UP:
        FSM_Messages_ENTTER_handler();
        break;
    default:
        break;
    }
}
void FSM_Measurement_struct_destroy(void)
{
    g_UIManager.current_screen->Cursor = 0;
}

//--------------------------------------------------------------------------//

void FSM_Channel_Setup_struct_create(void)
{
    TFT_menu_ALL_Clear();
    g_UIManager.current_screen->Cursor = 0;
    Menu_WriteStr(2, 0, "Parameter = TOC");
    TFT_menu_Arrow_write("<-");
    Loop_flag = 1;
}

void FSM_Channel_Setup_handle_state_while(void)
{
    static uint8_t lastcursor = 0;
    if (g_UIManager.current_screen->Cursor != lastcursor)
    {
        // 显示全部
        Menu_ClearChar(2, 12, 3);
        Menu_WriteStr(2, 12, "TOC");
        TFT_menu_Arrow_write("<-");
        goto exit;
    }
    if (Loop_flag)
    {
        if (g_UIManager.current_screen->Cursor == 0)
        {
            Menu_ClearChar(2, 12, 1);
            Menu_WriteStr(2, 12, "TOC");
        }
        else
        {
            TFT_menu_Arrow_write("<-");
        }

        Loop_flag = false;
    }
    else
    {
        if (g_UIManager.current_screen->Cursor == 0)
        {
            Menu_ClearChar(2, 12, 3);
            Menu_WriteStr(2, 12, "_");
        }
        else
        {
            TFT_menu_Arrow_Clear();
        }

        Loop_flag = true;
    }
exit:
    lastcursor = g_UIManager.current_screen->Cursor;
}
void FSM_Channel_Setup_handle_event(FSM_Event_t event)
{
    switch (event)
    {
    case FSM_Event_LEFT_PRESS_UP:
    case FSM_Event_RIGHT_PRESS_UP:
        g_UIManager.current_screen->Cursor = (g_UIManager.current_screen->Cursor + 1) % 2;
        break;
    case FSM_Event_ENTER_PRESS_UP:
        if (g_UIManager.current_screen->Cursor == 0)
        {
            UI_change_run(FSM_Channel_Setup_1);
        }
        else
        {
            UI_change_run(FSM_NONE);
        }
        break;
    default:
        break;
    }
}
void FSM_Channel_Setup_struct_destroy(void)
{
}

//--------------------------------------------------------------------------//

static void _FSM_Channel_Setup_1_set_class(uint8_t AB)
{
    if(AB != 0 || AB != 1)
    {
        return;
    }
    UI_Channel_Setup_1_Parameter_t *self = (UI_Channel_Setup_1_Parameter_t *)g_UIManager.current_screen->Parameter;
    switch (self->line[0].class)
    {
    case None:
        Menu_WriteStr(1, 3, "____");
        break;
    case TOC:
        Menu_WriteStr(1, 3, "TOC");
        break;
    case gC_L:
        Menu_WriteStr(1, 3, "gC/L");
        break;
    case o_cm:
        Menu_WriteStr(1, 3, "\xE9-cm");
        break;
    case S_cm:
        Menu_WriteStr(1, 3, "S/cm");
        break;
    case S_m:
        Menu_WriteStr(1, 3, "S/m");
        break;
    case oC:
        Menu_WriteStr(1, 3, "\xF7\x43");
        break;
    case oF:
        Menu_WriteStr(1, 3, "\xF7\x46");
        break;
    case LampRemain:
        Menu_WriteStr(1, 3, "LampRemain");
        break;

    }
}

static inline void _FSM_Channel_Setup_1_set_unit_a(void)
{
    // 暂时不实装 —— 无特殊处理
}

void FSM_Channel_Setup_1_struct_create(void)
{
    // 获取当前AB通道的值
    TFT_menu_ALL_Clear();
    Menu_WriteStr(1, 0, "a");
    Menu_WriteStr(2, 0, "b");
    TFT_menu_Arrow_write("<-");
}
void FSM_Channel_Setup_1_handle_state_while(void)
{
    static uint8_t lastcursor = 0;
    if (g_UIManager.current_screen->Cursor != lastcursor)
    {
        // 显示全部
        goto exit;
    }
    if (Loop_flag)
    {
        switch (g_UIManager.current_screen->Cursor)
        {
        case 0:
            // A类型
            _FSM_Channel_Setup_1_set_class(0);
            break;
        case 1:
            // A单位
            _FSM_Channel_Setup_1_set_unit_a();
            break;
        case 2:
            // B类型
            _FSM_Channel_Setup_1_set_class(1);
            break;
        case 3:
            // B单位
            break;
        case 4:
            // 放回
            break;
        default:
            break;
        }
        Loop_flag = false;
    }
    else
    {
        switch (g_UIManager.current_screen->Cursor)
        {
        case 0:
            // A类型
            Menu_ClearChar(1, 3, 10);
            break;
        case 1:
            // A单位
            break;
        case 3:
            // B类型
            break;
        case 4:
            // B单位
            Menu_ClearChar(2, 3, 10);
            break;
        case 5:
            // 放回
            break;
        default:
            break;
        }
        Loop_flag = true;
    }

exit:
    lastcursor = g_UIManager.current_screen->Cursor;
}
void FSM_Channel_Setup_1_handle_event(FSM_Event_t event)
{
    UI_Channel_Setup_1_Parameter_t *self = (UI_Channel_Setup_1_Parameter_t *)g_UIManager.current_screen->Parameter;
    switch (event)
    {
    case FSM_Event_LEFT_PRESS_UP:
        break;
    case FSM_Event_RIGHT_PRESS_UP:
        break;
    case FSM_Event_UP_PRESS_UP:
        break;
    case FSM_Event_DOWN_PRESS_UP:
        self->line[0].class = (enum Line_Class)((self->line[0].class + 1) % CLASS_LEN);
        break;
    case FSM_Event_ENTER_PRESS_UP:
        
        break;
    default:
        break;
    }
}
void FSM_Channel_Setup_1_struct_destroy(void)
{
}
